import redis
import configparser

cf = configparser.ConfigParser()
cf.read("conf/train.conf")

class Redis_Operation:
    def __init__(self, host=cf.get("redis_address","host"), port=cf.getint("redis_address","port"), 
                 db=cf.getint("redis_address","db")):
        self.r=redis.Redis(host=host, port=port, db=db)
    
    def insert_hash_data(self, key, model_name, data):
        self.r.hset(key, model_name, data)
        self.r.save()
        return 1
 
    def get_hash_data(self, key,  model_name):
        res = self.r.hget(key, model_name)
        return res    

    def del_hash_data(self, key, model_name):
        res = self.r.hdel(key, model_name)
        return 1
    
    def insert_into_train_queue(self, model_name):
        res = self.r.rpush("train_queue", model_name)
        return 1

    def pop_train_queue(self):
        res = self.r.lpop("train_queue")
        return res
    
    def get_data_by_index(self, index):
        res = self.r.lindex("train_queue", index) 
        return res
